package installer;

import helper.MysqlDatabase;

import java.io.*;
import java.util.Properties;

/**
 * @author Pascal de Vink
 *
 */
public class Dispatcher {

	private String server;
	private String database;
	private String username;
	private String password;
	private String path;
	private boolean log;
	private String logpath;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Dispatcher ds = new Dispatcher();
		ds.firstStep();
	}
	
	/**
	 * 
	 */
	public void firstStep() {
		try {
			this.getServername();
			this.getDatabasename();
			this.getUsername();
			this.getPassword();
			this.getPath();
			this.getLog();
		}
		catch (IOException ioe) {
			System.out.println("IO error reading");
			System.exit(1);
		}
		
		this.secondStep();
	}
	
	/**
	 * 
	 * @throws IOException
	 */
	private void getServername() throws IOException {
		System.out.println("Enter the database server [localhost]: ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		this.server = br.readLine();
		if (this.server.equals("")) {
			this.server = "localhost";
		}
	}
	
	/**
	 * 
	 * @throws IOException
	 */
	private void getDatabasename() throws IOException {
		System.out.println("Enter the database name [helios]: ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		this.database = br.readLine();
		if (this.database.equals("")) {
			this.database = "helios";
		}
	}
	
	/**
	 * 
	 * @throws IOException
	 */
	private void getUsername() throws IOException {
		System.out.println("Enter the database username [helios]: ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		this.username = br.readLine();
		if (this.username.equals("")) {
			this.username = "helios";
		}
	}
	
	/**
	 * 
	 * @throws IOException
	 */
	private void getPassword() throws IOException {
		System.out.println("Enter the database password []: ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		this.password = br.readLine();
		if (this.password.equals("")) {
			this.password = "";
		}
	}
	
	/**
	 * 
	 * @throws IOException
	 */
	private void getPath() throws IOException {
		System.out.println("Enter the music path [/var/music/]: ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		this.path = br.readLine();
		if (this.path.equals("")) {
			this.path = "/var/music/";
		}
	}
	
	/**
	 * 
	 * @throws IOException
	 */
	private void getLog() throws IOException {
		System.out.println("Would you like a log? y/n [y]: ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		if (br.readLine().equals("n")) {
			this.log = false;
		} else {
			this.log = true;
			
			System.out.println("Enter the log path [/var/log/helios]: ");
			br= new BufferedReader(new InputStreamReader(System.in));
			this.logpath = br.readLine();
			if (this.logpath.equals("")) {
				this.logpath = "/var/log/helios";
			}
		}
	}

	/**
	 * 
	 */
	private void secondStep() {
		System.out.println("You entered the following info:");
		System.out.println("Server: "+this.server);
		System.out.println("Database: "+this.database);
		System.out.println("Username: "+this.username);
		System.out.println("Password: "+this.password);
		System.out.println("Path: "+this.path);
		System.out.println("Log: "+this.log);
		if (this.log == true) {
			System.out.println("Logpath: "+this.logpath);
		}
		
		System.out.println("Is this correct? y/n [y]: ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		try {	
			if (br.readLine().equals("n")) {
				this.firstStep();
			} else {
				this.thirdStep();
			}
		}
		catch (IOException ioe) {
			System.out.println("IO error reading");
			System.exit(1);
		}
	}
	
	/**
	 * 
	 */
	private void thirdStep() {
		this.writeDatabase();
		this.writeConfig();
		System.exit(0);
	}
	
	private void writeDatabase() {
		String query;
		MysqlDatabase md = new MysqlDatabase(this.server, this.database, this.username, this.password);
		
		query = "CREATE TABLE IF NOT EXISTS `album` (" +
				"`id` int(11) NOT NULL auto_increment," +
				"`title` varchar(256) NOT NULL," +
				"`artist` int(11) NOT NULL," +
				"`folder` int(11) NOT NULL," +
				"PRIMARY KEY  (`id`)," +
				"UNIQUE KEY `title` (`title`)" +
				") ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
		md.execute(query);
		
		query = "CREATE TABLE IF NOT EXISTS `artist` (" +
				"`id` int(11) NOT NULL auto_increment," +
				"`name` varchar(256) NOT NULL," +
				"PRIMARY KEY  (`id`)," +
				"UNIQUE KEY `name` (`name`)" +
				") ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
		md.execute(query);

		query = "CREATE TABLE IF NOT EXISTS `file` (" +
				"`id` int(11) NOT NULL auto_increment," +
				"`name` text NOT NULL," +
				"`size` int(11) NOT NULL," +
				"`parent` int(11) NOT NULL," +
				"PRIMARY KEY  (`id`)" +
				") ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
		md.execute(query);

		query = "CREATE TABLE IF NOT EXISTS `folder` (" +
				"`id` int(11) NOT NULL auto_increment," +
				"`name` text NOT NULL," +
				"`parent` int(11) NOT NULL," +
				"PRIMARY KEY  (`id`)" +
				") ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
		md.execute(query);
		
		query = "CREATE TABLE IF NOT EXISTS `track` (" +
				"`id` int(11) NOT NULL auto_increment," +
				"`title` text NOT NULL," +
				"`artist` int(11) NOT NULL," +
				"`album` int(11) NOT NULL," +
				"`file` int(11) NOT NULL," +
				"PRIMARY KEY  (`id`)" +
				") ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
		md.execute(query);
		
		System.out.println("Database tables created.");
	}
	
	private void writeConfig() {
		try
	    {
			File file = new File("settings.properties");
			file.createNewFile();
			Properties p = new Properties();
	        p.load(new FileInputStream("settings.properties"));
	        p.put("path", this.path);
	        String url = "jdbc:mysql://"+this.server+":3306/"+this.database;
	        p.put("dburl", url);
	        p.put("dbuser", this.username);
	        p.put("dbpass", this.password);
	        p.put("logging", ""+this.log);
	        if (this.log == true) {
	        	p.put("logpath", this.logpath);
	        }
	        FileOutputStream out = new FileOutputStream("settings.properties");
	        p.store(out, "HeliosIndex properties");
	        System.out.println("Configuration file written. Now copy it to the same folder as HeliosIndex.jar");
	    }
		catch (Exception e) 
		{
	    	System.out.println(e);
	    	System.exit(1);
		}
	}
}
